<template>
	<view>
		<view class="cu-bar bg-white margin-top">
			<view class="action">
				图片上传
			</view>
			<view class="action">
				{{imgList.length}}/{{imgCount}}
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<imgCount">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	//目前实现是单张图片增删操作
	export default {
		props: {
			pageImgList: {
				type: Array,
				required: true,
				default () {
					return [];
				}
			},
			imgCount: {
				type: Number,
				default: 9,
			}
		},
		watch: {
			pageImgList(val) {
				this.imgList = [];
				this.imgList = this.imgList.concat(val);
			}
		},
		data() {
			return {
				imgList: [],
			}
		},
		mounted() {
			this.imgList = [];
			this.imgList = this.imgList.concat(this.pageImgList);
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: this.imgCount, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: (res) => {
						console.log(this.imgList);
						this.imgList = this.imgList.concat(res.tempFilePaths)
						const currentTempFilePath = res.tempFilePaths[0];
						this.$emit("addImg", currentTempFilePath);
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1);
							this.$emit("deleteImg", e.currentTarget.dataset.index);
						}
					}
				})
			},
		}
	}
</script>

<style scoped>
</style>
